package gsw.demo.pluggable.plugin_simple;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import gsw.demo.pluggable.R;

public class ActivityPluginSimple extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.jump1) {
                    startPluginActivity("gsw.demopluggable2.plugin_simple.ActivityPluginSimple1");
                } else if (v.getId() == R.id.jump2) {
                    startPluginActivity("gsw.demopluggable2.plugin_simple.ActivityPluginSimple2");
                } else if (v.getId() == R.id.jump3) {
                    startPluginActivity("gsw.demopluggable2.plugin_simple.ActivityPluginSimple3");
                }
            }
        };
        findViewById(R.id.jump1).setOnClickListener(clickListener);
        findViewById(R.id.jump2).setOnClickListener(clickListener);
        findViewById(R.id.jump3).setOnClickListener(clickListener);

        //一般在Application.onCreate()中调用，这里是为了测试
        PluginManagerSimple.getInstance().init(getApplicationContext());
    }

    /**
     * 根据类名，打开插件的Activity
     *
     * @param name 插件APK的Activity的类名
     */
    private void startPluginActivity(String name) {
        //插件APK的包名
        String pluginPackagenmae = "gsw.toolpluggabledemo2";
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(pluginPackagenmae, name));
        startActivity(intent);
    }

    /**
     * 退出登录
     */
    public void logout(View view) {
        SharedPreferences share = this.getSharedPreferences(HookManager.SPLOGIN, MODE_PRIVATE);//实例化
        SharedPreferences.Editor editor = share.edit(); //使处于可编辑状态
        editor.putBoolean(HookManager.SPLOGINSTATE, false);   //设置保存的数据
        Toast.makeText(this, "退出登录成功", Toast.LENGTH_SHORT).show();
        editor.commit();
    }
}
